<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
class MeritPayCut extends Controller{
	
    public function index()
    {
        return $this->fetch();
    }

    public function add(){

        //获取基础信息
        $user = Db('user')
            ->select();
        $this->assign('user', $user);

        return $this->fetch();
    }

    public function edit(){
        $id = request()->param('id');

        $info = Db('merit_pay_cut')

            ->where('merit_pay_cut.id','=',$id)

            ->find();
        $this->assign('info', $info);

        return $this->fetch();
    }

    public function getMeritPayCut(){
        $page = request()->param('page');
        $limit = request()->param('limit');
        $start = ($page-1) * $limit;
        //获取基础数据
        $infoList = array();
        $personInfoList = Db('base_info')
            ->join('user','base_info.user_id = user.id')
            ->join('company','base_info.company_id = company.id')
            ->select();
        //var_dump($personInfoList);
        foreach ($personInfoList as $personInfo){
            $meritPayCut = Db('merit_pay_cut')
                ->where([
                    'user_id'=> $personInfo['user_id'],
                    'isdelete' => 0
                ])
                ->find();
            
            if($meritPayCut != null){
                $info = array(
                    'id'=> $meritPayCut['id'],
                    'name' => $personInfo['name'],
                    'wage_date' => $meritPayCut['wage_date'],
                    'company' => $personInfo['company'],
                    'teaching_task_fee' => $meritPayCut['teaching_task_fee'],
                    'manage_fee' => $meritPayCut['manage_fee'],
                    'vacancy_subsidy' => $meritPayCut['vacancy_subsidy'],
                    'special_subsidy' => $meritPayCut['special_subsidy'],
                    'annual_assess_fee' => $meritPayCut['annual_assess_fee'],
                    'principal_award_fund' => $meritPayCut['principal_award_fund'],
                    'deduction_major_liability' => $meritPayCut['deduction_major_liability'],
                    'mediation_funds' => $meritPayCut['mediation_funds']
                );
                array_push($infoList,$info);
            }

        }


        $msg = [
            'code' => 0,
            'msg' => '查询成功',
            'data' => $infoList,
            'count' => count($infoList)
        ];
        
        echo json_encode($msg);

    }

    public function addPost(){
        //首先查询日期，单位是否相同，相同的话反馈错误
        $wage_date = request()->param("wage_date");
        $user_id = request()->param("user_id");
        $info = Db('merit_pay_cut')
            ->where([
                'user_id'    =>    ['=',$user_id],
                'wage_date'   =>    ['=',$wage_date],
            ])
            ->find();

        if($info == null){
            $id = Db('merit_pay_cut')->order('id desc')->limit(1)->find()['id'] +1;
            $status=Db('merit_pay_cut')
            ->insert([
                "id" => $id,
                "wage_date" => $wage_date,
                "user_id" => $user_id,
                "teaching_task_fee" => request()->param("teaching_task_fee"),
                "manage_fee" => request()->param("manage_fee"),
                "vacancy_subsidy" => request()->param("vacancy_subsidy"),
                "special_subsidy" => request()->param("special_subsidy"),
                'annual_assess_fee' => request()->param("annual_assess_fee"),
                'principal_award_fund' => request()->param("principal_award_fund"),
                'deduction_major_liability' => request()->param("deduction_major_liability"),
                'mediation_funds' => request()->param("mediation_funds"),
            ],true);
            Db('issue_date_year')->insert([
                "issue_date" => $wage_date
            ],true);
            if($status){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
            $this->error('添加失败,请查看是否有重复日期出现');
        }
    }

    public function editPost(){

        $id = request()->param("id");
        $status = Db('merit_pay_cut')
            ->where([
                'id'    =>    ['=',$id],
            ])
            ->update([
                "wage_date" => request()->param('wage_date'),
                "teaching_task_fee" => request()->param("teaching_task_fee"),
                "manage_fee" => request()->param("manage_fee"),
                "vacancy_subsidy" => request()->param("vacancy_subsidy"),
                "special_subsidy" => request()->param("special_subsidy"),
                'annual_assess_fee' => request()->param("annual_assess_fee"),
                'principal_award_fund' => request()->param("principal_award_fund"),
                'deduction_major_liability' => request()->param("deduction_major_liability"),
                'mediation_funds' => request()->param("mediation_funds")
            ]);

        if($status){
            $this->success('编辑成功');
        }else{
            $this->error('编辑失败');
        }
    }

}